package com.hui.account.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hui.account.R;
import com.hui.account.activity.RecordModActivity;
import com.hui.account.adapter.UserRecordAdapter;
import com.hui.account.database.SQLManage;
import com.hui.account.entity.RecordEntity;
import com.hui.account.entity.UserEntity;
import com.hui.account.utils.ButtonUtils;
import com.hui.account.utils.CommonUtils;
import com.hui.account.utils.ToastUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 客户欠款明细
 */
public class UserRecordActivity extends AppCompatActivity implements View.OnClickListener {

    Context context;

    Button backBtn;

    UserEntity user;

    ListView recordList;

    List<RecordEntity> data;

    UserRecordAdapter adapter;

    TextView countText, moneyText;

    SharedPreferences preferences; // preferences：共享参数，存储选中的用户信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        setContentView(R.layout.activity_user_record);

        // 1、设置屏幕顶部字体颜色
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        // 2、初始化页面
        initView();

        // 3、preferences：共享参数初始化
        preferences = getSharedPreferences("shareParam", Context.MODE_PRIVATE);
        String value = preferences.getString("selectUser", "");
        if (!TextUtils.isEmpty(value)) {
            user = new Gson().fromJson(value, UserEntity.class);
        } else {
            ToastUtils.error(context, "客户信息获取失败", Gravity.CENTER, 0, 0);
            finish();
        }

        // 4、设置 ListView点击事件
        onClickRecordList();

        // 5、引入用户欠款明细适配器
        data = new ArrayList<>();
        adapter = new UserRecordAdapter(this, data);
        recordList.setAdapter(adapter);
    }

    /**
     * 初始化页面
     */
    private void initView() {
        backBtn = findViewById(R.id.user_record_back);
        countText = findViewById(R.id.user_record_value1);
        moneyText = findViewById(R.id.user_record_value2);
        recordList = findViewById(R.id.user_records);

        backBtn.setOnClickListener(this);

    }

    /**
     * Activity加载时执行
     */
    @Override
    protected void onResume() {
        super.onResume();

        loadRecordData();
    }

    /**
     * 加载 欠款记录
     */
    @SuppressLint("SetTextI18n")
    private void loadRecordData() {
        List<RecordEntity> list = SQLManage.getUserRecords(0, 0, user.getId());
        if (list.size() > 0) {
            countText.setText(list.size() + CommonUtils.COUNT);

            BigDecimal totalMoney = new BigDecimal(0);
            for (RecordEntity r : list) {
                totalMoney = totalMoney.add(r.getMoney());
            }
            moneyText.setText(CommonUtils.MONEY + totalMoney);

            data.clear();
            data.addAll(list);
            adapter.notifyDataSetChanged();
        } else {
            data.clear();
            adapter.notifyDataSetChanged();
            finish();
        }

    }

    /**
     * 欠款单适配器点击事件
     */
    private void onClickRecordList() {
        recordList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!ButtonUtils.isFastDoubleClick()) {
                    // 1、获取到当前记录
                    RecordEntity record = data.get(i);

                    // 2、将数据存入共享参数
                    SharedPreferences.Editor edit = preferences.edit();
                    edit.putString("selectRecord", new Gson().toJson(record));
                    edit.commit();

                    startActivity(new Intent(context, RecordModActivity.class));
                }
            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_record_back:
                finish();
                break;
        }
    }
}